package gui;

import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import client.Main;
import client.MemberAccount;

	public class FreezeAccount_GUI extends JFrame implements ActionListener, TableModelListener{
		
		public MemberApprovalGUI mapg=null;
		public int uid = 0;
		public MemberAccount ma = null;
		public Object[][] data = null;
		public String[] columnNames = {"User ID","Name","Frozen"};
		
		
		public JTable table;
		JScrollPane scroll;
		
		//Buttons
		public JButton btnBack;
		public JButton btnLogOut;
		public JButton btnOK;
		
		//panels
		JPanel Mainpanel;
		
		//text
		JTextField txt;
		JTextArea textInput;
		
		//labels
		JLabel lblHeader;
		JLabel lblText;
		
	public FreezeAccount_GUI (){
			
			super();
			this.setSize(500,400);
			this.setBounds((int)Main.dim.getWidth()/2-260, (int)Main.dim.getHeight()/2-275, 515, 550);			
			this.setResizable(false);
			this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
			this.setTitle("Zer Lah - System Settings");
			this.add(getMainPanel());
			this.setVisible(true);
		}
	
	JPanel getMainPanel() 
	{	
		if (Mainpanel == null){
			Mainpanel = new JPanel();
			Mainpanel.setSize(500, 400);
			Mainpanel.setLayout(null);
			Mainpanel.add(getLabelHeader());
			Mainpanel.add(getBtnBack());
			Mainpanel.add(getBtnLogOut());	
			Mainpanel.add(getLblText());
			Mainpanel.add(getTextInput());
			Mainpanel.add(getBtnOK());
			//Mainpanel.add(getTable());
		}
		return Mainpanel;
	}
	
	JButton getBtnBack(){
		
		if (btnBack == null){
			btnBack = new JButton();
			btnBack.setBounds(380, 20, 100, 40);
			btnBack.setText("Back");
		}
		return btnBack;
	}
	
	JButton getBtnOK(){
		
		if(btnOK == null){
			btnOK = new JButton();
			btnOK.setBounds(420, 160, 60, 40);
			btnOK.setText("OK");
			btnOK.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(textInput.getText().isEmpty())
						JOptionPane.showMessageDialog(null, "You must insert a user ID!");
					else
					{
					uid = Integer.parseInt(textInput.getText());
					ma = new MemberAccount(uid);
					data = ma.OK(1);
					if(table!=null)
					{
						remove(table);
						remove(scroll);
						table.setVisible(false);
						scroll.setVisible(false);
						Mainpanel.repaint();
					}
					if(data.length == 1)
						Mainpanel.add(getTable());
					else
						JOptionPane.showMessageDialog(null, "Not a member!");
					}}
				});
		}
		return btnOK;
	}
	
	JButton getBtnLogOut() {
			
			if (btnLogOut == null){
				btnLogOut = new JButton();
				btnLogOut.setBounds(20,20,100,40);
				btnLogOut.addActionListener(this);
				btnLogOut.setText("LogOut");
			}
			return btnLogOut;
		}
	
	JLabel getLabelHeader()
	{
		if(lblHeader==null)
		{
			lblHeader=new JLabel();
			lblHeader.setText("Freeze/Unfreeze Account");
			lblHeader.setBounds(100, 100, 295, 40);
			lblHeader.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,25));
		}
		return lblHeader;
	}
	
	JLabel getLblText()
	{
		if(lblText==null)
		{
			lblText=new JLabel();
			lblText.setText("Insert the required user ID:");
			lblText.setBounds(15, 160, 220, 40);
			lblText.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		}
		return lblText;
	}
	
	JTextArea getTextInput()
	{
		textInput= new JTextArea(60,1);
		textInput.setBounds(246,160,160,40);
		textInput.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,18));
		return textInput;
	}
	
	JScrollPane getTable(){
		
		  DefaultTableModel model = new DefaultTableModel(data, columnNames){
			  public boolean isCellEditable(int rowIndex, int mColIndex) {
			      if(mColIndex!=2)
			    	  return false;
			      else return true;
			      }
		  };
		  table = new JTable(model); 
		  table.getModel().addTableModelListener(this);
		  TableColumn column = table.getColumnModel().getColumn(2);
		  column.setPreferredWidth(0);
		  column.setCellEditor(new DefaultCellEditor(new JCheckBox()));
		  table.getColumnModel().getColumn(2).setCellRenderer(
					new TableCellRenderer() {
	                            // the method gives the component  like whome the cell must be rendered
	                            public Component getTableCellRendererComponent(
								JTable table, Object value, boolean isSelected,
								boolean isFocused, int row, int col) {
							boolean marked = (Boolean) value;
							JCheckBox rendererComponent = new JCheckBox();
							if (marked) {
								rendererComponent.setSelected(true);
							}
							return rendererComponent;
						}
					});
		  
		  
		  column = table.getColumnModel().getColumn(0);
		  column.setPreferredWidth(20);
		  column = table.getColumnModel().getColumn(1);
		  column.setPreferredWidth(20);
	      scroll = new JScrollPane(table); 
	      scroll.setBounds(20, 240, 450, 100);
	      table.setBounds(20, 240, 450, 100);
	      return scroll; 
	}
	
	
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource() == btnLogOut)
		{
			if(JOptionPane.showConfirmDialog(null,"Are you sure you want to logout?",null,JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
			{
				this.setEnabled(false);
				this.setVisible(false);
				Main.main.logOut.doClick();
			}
		}
	}
	
public void tableChanged(TableModelEvent e) {
		
		TableModel model = null;
		model = (TableModel)e.getSource();
		int row = e.getFirstRow();
		int column=e.getColumn();
		Boolean data = (Boolean) model.getValueAt(row, column);
		String a = new Boolean(data).toString();
		Object uid = model.getValueAt(row, 0);
		int id = new Integer(uid.toString());
		if(column==2){
			ma = new MemberAccount(uid);
			ma.changeFrozen(a);
		if(data.equals(true))
			JOptionPane.showMessageDialog(null, "Account of User "+id+"  has been successfully frozen!");
		else
			JOptionPane.showMessageDialog(null, "Account of User "+id+" has been successfully recovered!");
		
		}
		
    }


}
